//
//  ByCellSettingAddressItem.m
//  mijifen_ios
//
//  Created by 朱凯 on 2017/10/23.
//  Copyright © 2017年 itboye. All rights reserved.
//

#import "ByCellAddressListItem.h"

// view
#import "ByViewAddressDefaultToggle.h"

@interface ByCellAddressListItem ()

@end

@implementation ByCellAddressListItem
{
    UILabel *_nameLabel;
    UILabel *_mobileLabel;
    UILabel *_addressLabel;
    ByViewAddressDefaultToggle *_toggle;
}
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    if (self == [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        [self setupUI];
    }
    return self;
}

- (void)setupUI {
    
    // 名字
    _nameLabel = [UILabel new];
    _nameLabel.font = FONT(16);
    [self.contentView addSubview:_nameLabel];
    
    // 手机
    _mobileLabel = [UILabel new];
    _mobileLabel.font = FONT(16);
    _mobileLabel.textAlignment = NSTextAlignmentRight;
    [self.contentView addSubview:_mobileLabel];
   
    // 地址
    _addressLabel = [UILabel new];
    _addressLabel.numberOfLines = 0;
    _addressLabel.font = FONT(16);
    [self.contentView addSubview:_addressLabel];
    
    // 线条
    UIView *line = [UIView new];
    line.backgroundColor = BY_LINE_COLOR;
    [self.contentView addSubview:line];
    
    // 默认地址toggle
    _toggle = [ByViewAddressDefaultToggle new];
    [_toggle addTarget:self action:@selector(defaultToggle) forControlEvents:UIControlEventTouchUpInside];
    [self.contentView addSubview:_toggle];
    
    // 删除按钮
    UIControl *deleteControl = [UIControl new];
    [deleteControl addTarget:self action:@selector(delete) forControlEvents:UIControlEventTouchUpInside];
    [self.contentView addSubview:deleteControl];
    //
    UILabel *deleteLabel = [[UILabel alloc] init];
    deleteLabel.text = @"删除";
    deleteLabel.font = FONT(14);
    deleteLabel.textAlignment = NSTextAlignmentRight;
    deleteLabel.textColor = [UIColor blackColor];
    [deleteControl addSubview:deleteLabel];
    //
    UIButton *deleteButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [deleteButton setImage:[UIImage imageNamed:@"地址删除"] forState:UIControlStateNormal];
    [deleteControl addSubview:deleteButton];
    
    // 修改按钮
    UIControl *modifyControl = [UIControl new];
    [modifyControl addTarget:self action:@selector(modify) forControlEvents:UIControlEventTouchUpInside];
    [self.contentView addSubview:modifyControl];
    //
    UILabel *modifyLabel = [[UILabel alloc] init];
    modifyLabel.text = @"修改";
    modifyLabel.font = FONT(14);
    modifyLabel.textAlignment = NSTextAlignmentRight;
    modifyLabel.textColor = [UIColor blackColor];
    [modifyControl addSubview:modifyLabel];
    //
    UIButton *modifyButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [modifyButton setImage:[UIImage imageNamed:@"地址修改"] forState:UIControlStateNormal];
    [modifyControl addSubview:modifyButton];
   
    
    [_nameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.equalTo(self.contentView).offset(10);
        make.height.equalTo(@20);
    }];
    [_mobileLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.contentView).offset(10);
        make.height.equalTo(_nameLabel);
        make.right.equalTo(self.contentView).offset(-10);
        make.left.equalTo(_nameLabel.mas_right).offset(10);
    }];
    [_addressLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(_nameLabel.mas_bottom).offset(10);
        make.left.equalTo(_nameLabel);
        make.right.equalTo(_mobileLabel);
    }];
    [line mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(self.contentView);
        make.top.equalTo(_addressLabel.mas_bottom).offset(10);
        make.height.equalTo(@1);
    }];
    [_toggle mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(_addressLabel);
        make.top.equalTo(line.mas_bottom);
        make.bottom.equalTo(self.contentView);
        make.width.equalTo(@100);
        make.height.equalTo(@44);
    }];
    [deleteControl mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self.contentView).offset(-10);
        make.top.bottom.equalTo(_toggle);
    }];
    [deleteLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.top.bottom.equalTo(deleteControl);
    }];
    [deleteButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(CGSizeMake(20, 20));
        make.right.equalTo(deleteLabel.mas_left).offset(-5);
        make.centerY.equalTo(deleteLabel);
        make.left.equalTo(deleteControl);
    }];
    [modifyControl mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(deleteControl.mas_left).offset(-10);
        make.top.bottom.equalTo(_toggle);
    }];
    [modifyLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.top.bottom.equalTo(modifyControl);
    }];
    [modifyButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(CGSizeMake(20, 20));
        make.right.equalTo(modifyLabel.mas_left).offset(-5);
        make.centerY.equalTo(modifyLabel);
        make.left.equalTo(modifyControl);
    }];
    
}


- (void)setModel:(ByModelAddress *)model {
    
    _model = model;
    
    _nameLabel.text = _model.name;
    _mobileLabel.text = _model.mobile;
    _addressLabel.text = _model.address;
    _toggle.isDefault = _model.isDefault;

}
- (void)modify {
    
}
- (void)delete {
    
}
- (void)defaultToggle {
    _toggle.isDefault = true;
}

@end
